package zcxn.microService.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.channels.FileChannel;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.xeustechnologies.jtar.TarEntry;
import org.xeustechnologies.jtar.TarOutputStream;

import zcxn.microService.bean.GlobalAllConfigInfo;

public class DoMicroServiceFile {
//	写启动脚本
	public static void writeStartServiceShell(GlobalAllConfigInfo allConfigInfo) throws IOException {
		if (allConfigInfo==null){
			return;
		}
		String startFilePath="e:/PackageMicroService/".concat(allConfigInfo.getServiceName()).concat("/").concat(allConfigInfo.getServiceName()).concat(".sh");
		String charSet="UTF-8";
		//写字符转换成字节流
		FileOutputStream fileWriter=new FileOutputStream(startFilePath);
		OutputStreamWriter writer=new OutputStreamWriter(fileWriter, charSet);
		try {
			writer.write("start=\"./".concat(allConfigInfo.getServiceName()).concat(".sh -c Config").concat(allConfigInfo.getGridId()+".yaml\""));
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			writer.close();
		}
	}
//	将二进制文件copy到该服务名文件下
	public void copyFileToServiceNameFile(String serviceName,int batType) throws IOException {
		String filePathString=null;
		if (batType==3){
			filePathString="E:\\PackageMicroService\\QianTanDianChi\\3c4544d203d294da9166dae947e49f34_dev_ttyUSB1";
		}else if (batType==1) {
			filePathString="E:\\PackageMicroService\\LiDianChi\\3c4544d203d294da9166dae947e49f34_dev_ttyUSB1";
		}
		copyFileUsingFileChannels(new File(filePathString),new File("e:/PackageMicroService/".concat(serviceName+"/"+serviceName)));
	}
	
//文件copy
private static void copyFileUsingFileChannels(File source, File dest) throws IOException {    
        FileChannel inputChannel = null;    
        FileChannel outputChannel = null;    
    try {
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } finally {
        inputChannel.close();
        outputChannel.close();
    }
}
//E:\PackageMicroService\25562c127c335d805f69ba57fe92a3a9_dev_ttyS1
//3个文件打包
public static File tarFiles(String servcieName, String gridId) {
	String yamlpath = "E:" + File.separator + "PackageMicroService" + File.separator + "".concat(servcieName)
			+ File.separator + "Config".concat(gridId + "").concat(".yaml");
	String buildPath = "E:" + File.separator + "PackageMicroService" + File.separator + "".concat(servcieName)
			+ File.separator + servcieName;
	String startPath = "E:" + File.separator + "PackageMicroService" + File.separator + "".concat(servcieName)
			+ File.separator + servcieName.concat(".sh");
	
	File target = new File(startPath.concat(".tgz"));
//	try {
//		out = new FileOutputStream(target);
//	} catch (FileNotFoundException e1) {
//		e1.printStackTrace();
//	}
//	TarArchiveOutputStream os = new TarArchiveOutputStream(out);
//	for (File file : sources) {
//		try {
//			os.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); 
//			os.putArchiveEntry(new TarArchiveEntry(file));
//			IOUtils.copy(new FileInputStream(file), os);
//			os.closeArchiveEntry();
//
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
//	if (os != null) {
//		try {
//			os.flush();
//			os.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
	TarOutputStream out = null;
	FileOutputStream dest=null;
	try {
		dest = new FileOutputStream(startPath.concat(".tgz"));
		out = new TarOutputStream(new BufferedOutputStream(dest));
		File[] filesToTar = new File[3];
		filesToTar[0] = new File(yamlpath);
		filesToTar[1] = new File(buildPath);
		filesToTar[2] = new File(startPath);
		for (File f : filesToTar) {
			out.putNextEntry(new TarEntry(f, f.getName()));
			BufferedInputStream origin = new BufferedInputStream(new FileInputStream(f));
			int count;
			byte data[] = new byte[2048];
			while ((count = origin.read(data)) != -1) {
				out.write(data, 0, count);
			}
			out.flush();
			origin.close();
		}
		out.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}

	return target;
}
	
}
